home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / spoc88 / proasm / read.asm < prev   
Assembly Source File  |  1988-06-09  |  4KB  |  111 lines

  1. COMMENT * ------------------------------------------------------------
  2.  
  3. This program receives an integer on the stack that refers to
  4. an open file.  This number is called a file handle.  The
  5. Turbo Prolog program gets the file handle by calling the
  6. assembly language predicate "open".  The FileHANDLE,
  7. itself, appears at offset 14 on the stack.  
  8.  
  9.  BOTTOM OF STACK
  10.    ------- 
  11.    |  15  |
  12.    |  14  | <--- 1st PARM (INPUT) FileHANDLE  ( 2 bytes)
  13.    |  13  |
  14.    |  12  |
  15.    |  11  |
  16.    |  10  | <--- Address of 2nd PARM (OUTPUT)  NumBYTESread
  17.    |  9   |
  18.    |  8   |
  19.    |  7   |
  20.    |  6   | <----Address of 3rd PARM (OUTPUT) DataBUF (4 byte pointer)
  21.    |  5   |                      
  22.    |  4   |
  23.    |  3   |
  24.    |  2   | <--RETURN ADDRESS
  25.    |  1   |
  26.    |  0   | <--BP
  27.    --------
  28.   TOP OF STACK
  29.  
  30. ---------------------------------------------------------------------*
  31.  
  32. A_PROG       SEGMENT BYTE
  33.            ASSUME CS: A_PROG
  34.            ASSUME DS: A_PROG 
  35. PUBLIC       read_0
  36. read_0   PROC   FAR
  37.            MOV AH,3Fh                 ; AH = 3F means read file
  38.            INT 3                      ; This causes program to halt
  39.                                       ; under debug
  40.  
  41.  
  42.            PUSH BP                    ; Save base pointer on stack
  43.            MOV BP,SP                  ; Set BP to SP
  44.            MOV SI,DS                  ; Save Turbo Prolog program's 
  45.                                       ; data segment address
  46.  
  47.  
  48.  
  49.            MOV CX,1                   ; CX must contain number
  50.                                       ; of bytes to read
  51.  
  52. ;          Set DS:DX to point to addrress DataBUF 
  53.  
  54.            MOV DS,[BP]+8              ;Put high word in DS
  55.            MOV DX,[BP]+6              ;Put low word in DX
  56.  
  57.  
  58.            MOV BX, [BP]+ 14           ; get file handle from stack
  59.  
  60. ;          Before calling INT 21, make sure these conditions
  61. ;          are satisfied:
  62. ;             AH = 3FH
  63. ;             BX = file handle
  64. ;             CX = number of bytes to read
  65. ;             DS:DX point to the address of the input buffer 
  66. ;
  67.  
  68.            INT 21h
  69.            JC  FAILURE                ; If carry flag is set
  70.                                       ; jump to FAILURE
  71.  
  72. ;          Zero out the hibyte of the DataBUF variable 
  73.  
  74.            MOV DI,DX                 ; DI -> lowbyte
  75.            INC DI                    ; Make DI point to hibyte
  76.            MOV BYTE PTR[DI],00h      ; Move zero to hibyte
  77.  
  78. ;          AX shows the number of bytes read.  So now we
  79. ;          return this information to Prolog
  80.  
  81.            LDS DI,DWORD PTR [BP] +10  ; Make DS:DI point to
  82.                                       ; the NumBYTESread variable.
  83.            MOV [DI],AX                ; Move AX to NumBYTESread
  84.            
  85.            POP BP                     ; Restore the Base Pointer  
  86.            MOV DS,SI                  ; Restore calling progs data
  87.                                       ; data segment
  88.            RET 10                     ; Pop the parms off the stack
  89.                                       ; 4+4+2
  90. FAILURE:
  91.            MOV AX,0FFh                ; 0FFh is hex for -1
  92.            LDS DI,DWORD PTR [BP] + 10 ; Set NumBYTESread to -1
  93.            MOV [DI],AX                ; To show read failure
  94.            POP BP                     ; Restore BP
  95.            MOV DS,SI                  ; Restore DS
  96.            RET 10                     ; Pop the parms off the stack
  97. ; The 10 after RET is critical.  Without it, the return to
  98. ; Turbo Prolog will be fouled up.
  99. ;
  100. ;  We put 10 after RET here because we need to pop the parameters
  101. ;  that were pushed on the stack when this routine was called.
  102. ;  This routine receives 3 arguments on the stack:
  103. ;       FileHANLE    (2 bytes)
  104. ;       NumBYTESread (4 byte pointer)
  105. ;       DataBUFF     (4 byte pointer)
  106. ;  2+4+4 = 10, hence the 10 after RET
  107.  
  108. read_0   ENDP
  109. A_PROG     ENDS
  110. END
  111.